package adv.np.pViaggianti.view.gwt.client.views;

import static adv.np.pViaggianti.view.gwt.client.localization.LocaleSettings.commonMessages;
import adv.np.pViaggianti.view.gwt.client.authentication.AuthorizationProvider;
import adv.np.pViaggianti.view.gwt.client.commons.BaseView;
import adv.np.pViaggianti.view.gwt.client.dispatchers.LoginDispatcher;
import adv.np.pViaggianti.view.gwt.client.helpers.Utils;
import adv.np.pViaggianti.view.gwt.client.valueObjects.LoginVO;

import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.HeaderItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * <p>
 * Il Login-Presentation dell'applicazione Partite Viaggianti.
 * </p>
 * <p>
 * Questa deve essere l'interfaccia del pannello iniziale dell'applicazione, al
 * momento permette unicamente il logIn
 * </p>
 * 
 * @author Giovanni Dall'Oglio Risso
 */
public class LoginView extends BaseView {

	private LoginDispatcher myController;

	public LoginView(LoginDispatcher controller) {
		myController = controller;
		
	}

	@Override
	public VisualizationMode getVisualizationMode() {
		return VisualizationMode.FULLSCREEN;
	}

	private TextItem userItem = new TextItem("User");
	private PasswordItem passItem = new PasswordItem("Password");

	private DynamicForm df = new DynamicForm();
	
	private Label msgErrLabel = new Label();
	public Label getMsgErrLabel() {
		return msgErrLabel;
	}

	public void setMsgErrLabel(Label msgErrLabel) {
		this.msgErrLabel = msgErrLabel;
	}

	@Override
	public Widget createWidget() {

		if (AuthorizationProvider.isLogged()) {
			userItem.setValue(AuthorizationProvider.getAuthInfo().getUserInfo()
					.getUserName());

		}

		df.setWidth(300);
		df.setHeight(60);

		msgErrLabel.setWidth(300);
		msgErrLabel.setHeight(10);
		msgErrLabel.setAlign(Alignment.CENTER);
		

		userItem.setTitle(commonMessages.txtUserName());
		userItem.setWidth("*");
		userItem.setValidators(Utils.getRequiredValidator());
		
		
		userItem.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					passItem.focusInItem();
				}
			}
		});

		passItem.setTitle(commonMessages.txtPassword());
		passItem.setWidth("*");		
		passItem.setValidators(Utils.getRequiredValidator());
		
		

		passItem.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					callLogIn();
				}
			}
		});

		HeaderItem header = new HeaderItem();  
        header.setDefaultValue(commonMessages.txtLogInTitle());
		header.setAlign(Alignment.RIGHT);
        
        df.setPadding(15);
        
		df.setFields(header, userItem, passItem);

		Button clear = new Button(commonMessages.btnClear());
		clear.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				userItem.setValue("");
				passItem.setValue("");
				msgErrLabel.setTitle("");
			}
		});

		Button go = new Button(commonMessages.btnLogin());
		go.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				callLogIn();
			}
		});


		VLayout verticaPanel = new VLayout();
		
		HLayout buttons = new HLayout();

		LayoutSpacer lspac = new LayoutSpacer();
		lspac.setWidth(55);
		
		buttons.setPadding(15);		
		buttons.addMember(clear);
		buttons.addMember(lspac);
		buttons.addMember(go);
		
		verticaPanel.setBorder("1px solid black");
		verticaPanel.addMember(df);
		verticaPanel.addMember(msgErrLabel);
		verticaPanel.addMember(buttons);


		HLayout panel = new HLayout();
        
		panel.setPadding(15);
		panel.addMember(verticaPanel);
		return panel;
	}

	/**
	 * Informa il proprio dispatcher che va effettuato il logIn
	 */
	private void callLogIn() {
		msgErrLabel.setContents("");
		if (df.validate(false)) {
			LoginVO lvo = new LoginVO();
			lvo.setUserName(userItem.getValue().toString());
			lvo.setPassword(passItem.getValue().toString());
			myController.performLogIn(lvo);
		}
	}

	
	

}
